home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / filelock.c < prev    next >
C/C++ Source or Header  |  1991-12-02  |  9KB  |  354 lines

  1. /* Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  2.  
  3. This file is part of GNU Emacs.
  4.  
  5. GNU Emacs is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GNU Emacs is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Emacs; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include "config.h"
  23. #ifdef hpux
  24. /* needed by <pwd.h> */
  25. #include <stdio.h>
  26. #undef NULL
  27. #endif
  28. #include "lisp.h"
  29. #include "paths.h"
  30. #include "buffer.h"
  31. #include <pwd.h>
  32. #include <errno.h>
  33. #include <sys/file.h>
  34. #ifdef USG
  35. #include <fcntl.h>
  36. #endif /* USG */
  37.  
  38. extern int errno;
  39.  
  40. #ifdef CLASH_DETECTION
  41.   
  42. /* If system does not have symbolic links, it does not have lstat.
  43.    In that case, use ordinary stat instead.  */
  44.  
  45. #ifndef S_IFLNK
  46. #define lstat stat
  47. #endif
  48.  
  49. static Lisp_Object
  50. lock_file_owner_name (lfname)
  51. {
  52.   struct stat s;
  53.   struct passwd *the_pw;
  54.   extern struct passwd *getpwuid ();
  55.  
  56.   if (lstat (lfname, &s) == 0)
  57.     the_pw = getpwuid (s.st_uid);
  58.   return (the_pw == 0 ? Qnil : build_string (the_pw->pw_name));
  59. }
  60.  
  61.  
  62. /* lock_file locks file fn,
  63.    meaning it serves notice on the world that you intend to edit that file.
  64.    This should be done only when about to modify a file-visiting
  65.    buffer previously unmodified.
  66.    Do not (normally) call lock_buffer for a buffer already modified,
  67.    as either the file is already locked, or the user has already
  68.    decided to go ahead without locking.
  69.  
  70.    When lock_buffer returns, either the lock is locked for us,
  71.    or the user has said to go ahead without locking.
  72.  
  73.    If the file is locked by someone else, lock_buffer calls
  74.    ask-user-about-lock (a Lisp function) with two arguments,
  75.    the file name and the name of the user who did the locking.
  76.    This function can signal an error, or return t meaning
  77.    take away the lock, or return nil meaning ignore the lock.  */
  78.  
  79. /* The lock file name is the file name with "/" replaced by "!"
  80.    and put in the Emacs lock directory.  */
  81. /* (ie., /ka/king/junk.tex -> /!/!ka!king!junk.tex). */
  82.  
  83. void
  84. lock_file (fn)
  85.      register Lisp_Object fn;
  86. {
  87.   register Lisp_Object attack;
  88.   register char *lfname;
  89.  
  90.   /* Create the name of the lock-file for file fn */
  91.   lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
  92.   fill_in_lock_file_name (lfname, fn);
  93.  
  94.   /* See if this file is visited and has changed on disk since it was visited.  */
  95.   {
  96.     register Lisp_Object subject_buf = Fget_file_buffer (fn);
  97.     if (!NULL (subject_buf)
  98.     && NULL (Fverify_visited_file_modtime (subject_buf))
  99.     && !NULL (Ffile_exists_p (fn)))
  100.       call1 (intern ("ask-user-about-supersession-threat"), fn);
  101.   }
  102.  
  103.   /* Try to lock the lock. */
  104.   if (lock_if_free (lfname) <= 0)
  105.     /* Return now if we have locked it, or if lock dir does not exist */
  106.     return;
  107.  
  108.   /* Else consider breaking the lock */
  109.   attack = call2 (intern ("ask-user-about-lock"), fn,
  110.           lock_file_owner_name (lfname));
  111.   if (!NULL (attack))
  112.     /* User says take the lock */
  113.     {
  114.       lock_superlock (lfname);
  115.       lock_file_1 (lfname, O_WRONLY) ;
  116.       unlink (PATH_SUPERLOCK);
  117.       return;
  118.     }
  119.   /* User says ignore the lock */
  120. }
  121.  
  122. fill_in_lock_file_name (lockfile, fn)
  123.      register char *lockfile;
  124.      register Lisp_Object fn;
  125. {
  126.   register char *p;
  127.  
  128.   strcpy (lockfile, PATH_LOCK);
  129.  
  130.   p = lockfile + strlen (lockfile);
  131.  
  132.   strcpy (p, XSTRING (fn)->data);
  133.  
  134.   for (; *p; p++)
  135.     {
  136.       if (*p == '/')
  137.     *p = '!';
  138.     }
  139. }
  140.  
  141. /* Lock the lock file named LFNAME.
  142.    If MODE is O_WRONLY, we do so even if it is already locked.
  143.    If MODE is O_WRONLY | O_EXCL | O_CREAT, we do so only if it is free.
  144.    Return 1 if successful, 0 if not.  */
  145.  
  146. int
  147. lock_file_1 (lfname, mode)
  148.      int mode; char *lfname; 
  149. {
  150.   register int fd;
  151.   char buf[20];
  152.  
  153.   if ((fd = open (lfname, mode, 0666)) >= 0)
  154.     {
  155.       fchmod (fd, 0666);
  156.       sprintf (buf, "%d ", getpid ());
  157.       write (fd, buf, strlen (buf));
  158.       close (fd);
  159.       return 1;
  160.     }
  161.   else
  162.     return 0;
  163. }
  164.  
  165. /* Lock the lock named LFNAME if possible.
  166.    Return 0 in that case.
  167.    Return positive if lock is really locked by someone else.
  168.    Return -1 if cannot lock for any other reason.  */
  169.  
  170. int
  171. lock_if_free (lfname)
  172.      register char *lfname; 
  173. {
  174.   register int clasher;
  175.  
  176.   while (lock_file_1 (lfname, O_WRONLY | O_EXCL | O_CREAT) == 0)
  177.     {
  178.       if (errno != EEXIST)
  179.     return -1;
  180.       clasher = current_lock_owner (lfname);
  181.       if (clasher != 0)
  182.     if (clasher != getpid ())
  183.       return (clasher);
  184.     else return (0);
  185.       /* Try again to lock it */
  186.     }
  187.   return 0;
  188. }
  189.  
  190. /* Return the pid of the process that claims to own the lock file LFNAME,
  191.    or 0 if nobody does or the lock is obsolete,
  192.    or -1 if something is wrong with the locking mechanism.  */
  193.  
  194. int
  195. current_lock_owner (lfname)
  196.      char *lfname;
  197. {
  198.   int owner = current_lock_owner_1 (lfname);
  199.   if (owner == 0 && errno == ENOENT)
  200.     return (0);
  201.   /* Is it locked by a process that exists?  */
  202.   if (owner != 0 && (kill (owner, 0) >= 0 || errno == EPERM))
  203.     return (owner);
  204.   if (unlink (lfname) < 0)
  205.     return (-1);
  206.   return (0);
  207. }
  208.  
  209. int
  210. current_lock_owner_1 (lfname)
  211.      char *lfname;
  212. {
  213.   register int fd;
  214.   char buf[20];
  215.   int tem;
  216.  
  217.   fd = open (lfname, O_RDONLY, 0666);
  218.   if (fd < 0)
  219.     return 0;
  220.   tem = read (fd, buf, sizeof buf);
  221.   close (fd);
  222.   return (tem <= 0 ? 0 : atoi (buf));
  223. }
  224.  
  225.  
  226. void
  227. unlock_file (fn)
  228.      register Lisp_Object fn;
  229. {
  230.   register char *lfname;
  231.  
  232.   lfname = (char *) alloca (XSTRING (fn)->size + strlen (PATH_LOCK) + 1);
  233.   fill_in_lock_file_name (lfname, fn);
  234.  
  235.   lock_superlock (lfname);
  236.  
  237.   if (current_lock_owner_1 (lfname) == getpid ())
  238.     unlink (lfname);
  239.  
  240.   unlink (PATH_SUPERLOCK);
  241. }
  242.  
  243. lock_superlock (lfname)
  244.      char *lfname;
  245. {
  246.   register int i, fd;
  247.  
  248.   for (i = -20; i < 0 && (fd = open (PATH_SUPERLOCK,
  249.                      O_WRONLY | O_EXCL | O_CREAT, 0666)) < 0;
  250.        i++)
  251.     {
  252.       if (errno != EEXIST)
  253.     return;
  254.       sleep (1);
  255.     }
  256.   if (fd >= 0)
  257.     {
  258.       fchmod (fd, 0666);
  259.       write (fd, lfname, strlen (lfname));
  260.       close (fd);
  261.     }
  262. }
  263.  
  264. void
  265. unlock_all_files ()
  266. {
  267.   register Lisp_Object tail;
  268.   register struct buffer *b;
  269.  
  270.   for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
  271.        tail = XCONS (tail)->cdr)
  272.     {
  273.       b = XBUFFER (XCONS (XCONS (tail)->car)->cdr);
  274.       if (XTYPE (b->filename) == Lisp_String &&
  275.       b->save_modified < BUF_MODIFF (b))
  276.     unlock_file (b->filename);
  277.     }
  278. }
  279.  
  280.  
  281. DEFUN ("lock-buffer", Flock_buffer, Slock_buffer,
  282.   0, 1, 0,
  283.   "Locks FILE, if current buffer is modified.\n\
  284. FILE defaults to current buffer's visited file,\n\
  285. or else nothing is done if current buffer isn't visiting a file.")
  286.   (fn)
  287.      Lisp_Object fn;
  288. {
  289.   if (NULL (fn))
  290.     fn = current_buffer->filename;
  291.   else
  292.     CHECK_STRING (fn, 0);
  293.   if (current_buffer->save_modified < MODIFF
  294.       && !NULL (fn))
  295.     lock_file (fn);
  296.   return Qnil;    
  297. }
  298.  
  299. DEFUN ("unlock-buffer", Funlock_buffer, Sunlock_buffer,
  300.   0, 0, 0,
  301.  "Unlocks the file visited in the current buffer,\n\
  302. if it should normally be locked.")
  303.   ()
  304. {
  305.   if (current_buffer->save_modified < MODIFF &&
  306.       XTYPE (current_buffer->filename) == Lisp_String)
  307.     unlock_file (current_buffer->filename);
  308.   return Qnil;
  309. }
  310.  
  311.  
  312. /* Unlock the file visited in buffer BUFFER.  */
  313.  
  314. unlock_buffer (buffer)
  315.      struct buffer *buffer;
  316. {
  317.   if (buffer->save_modified < BUF_MODIFF (buffer)
  318.       && XTYPE (buffer->filename) == Lisp_String)
  319.     unlock_file (buffer->filename);
  320. }
  321.  
  322. DEFUN ("file-locked-p", Ffile_locked_p, Sfile_locked_p, 0, 1, 0,
  323.   "Returns nil if the FILENAME is not locked,\n\
  324. t if it is locked by you, else a string of the name of the locker.")
  325.   (fn)
  326.   Lisp_Object fn;
  327. {
  328.   register char *lfname;
  329.   int owner;
  330.  
  331.   fn = Fexpand_file_name (fn, Qnil);
  332.  
  333.   /* Create the name of the lock-file for file filename